//
// Created by msi on 2025/8/8.
//

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include "global_defination/global_defination.h"

int main() {
    std::string folder = WORK_SPACE_PATH + "/csv_test/bin/";
    std::vector<std::string> keep_columns = {
        "seconds_from_epoch", "nanoseconds",
        "body_rpy_0", "body_rpy_1", "body_rpy_2",
        "rpy_setpoint_0", "rpy_setpoint_1", "rpy_setpoint_2",
        "gyro_corrected_0", "gyro_corrected_1", "gyro_corrected_2",
        "Torque_0", "Torque_1", "Torque_2",
        "Torque_body_fb_0", "Torque_body_fb_1", "Torque_body_fb_2",
        "motor_currents[0]", "motor_currents[1]", "motor_currents[2]",
        "sensor_data.motor_rpm_speeds_[0]", "sensor_data.motor_rpm_speeds_[1]", "sensor_data.motor_rpm_speeds_[2]",
    };

    // 获取目录下所有csv文件
    std::vector<std::string> files;
    std::string cmd = "ls " + folder + "*.csv";
    FILE* pipe = popen(cmd.c_str(), "r");
    if (!pipe) {
        std::cerr << "无法读取目录: " << folder << std::endl;
        return 1;
    }
    char buffer[512];
    while (fgets(buffer, sizeof(buffer), pipe)) {
        std::string f(buffer);
        f.erase(std::remove(f.begin(), f.end(), '\n'), f.end());
        files.push_back(f);
    }
    pclose(pipe);

    for (const auto& infile_path : files) {
        std::ifstream infile(infile_path);
        if (!infile) {
            std::cerr << "无法打开文件: " << infile_path << std::endl;
            continue;
        }
        std::vector<std::string> all_lines;
        std::string line;
        while (std::getline(infile, line)) {
            all_lines.push_back(line);
        }
        infile.close();
        if (all_lines.size() <= 2) {
            std::cerr << "文件行数不足，无法删除最后两行: " << infile_path << std::endl;
            continue;
        }
        std::string outfile_path = infile_path.substr(0, infile_path.size()-4) + "_filter.csv";
        std::ofstream outfile(outfile_path);
        if (!outfile) {
            std::cerr << "无法写入文件: " << outfile_path << std::endl;
            continue;
        }
        std::vector<int> col_indices;
        bool header_processed = false;
        for (size_t idx = 0; idx < all_lines.size() - 2; ++idx) {
            std::stringstream ss(all_lines[idx]);
            std::string cell;
            std::vector<std::string> cells;
            while (std::getline(ss, cell, ',')) {
                cells.push_back(cell);
            }
            if (!header_processed) {
                for (size_t i = 0; i < cells.size(); ++i) {
                    if (std::find(keep_columns.begin(), keep_columns.end(), cells[i]) != keep_columns.end()) {
                        col_indices.push_back(i);
                    }
                }
                for (size_t i = 0; i < col_indices.size(); ++i) {
                    outfile << cells[col_indices[i]];
                    if (i < col_indices.size() - 1) outfile << ",";
                }
                outfile << "\n";
                header_processed = true;
            } else {
                for (size_t i = 0; i < col_indices.size(); ++i) {
                    outfile << cells[col_indices[i]];
                    if (i < col_indices.size() - 1) outfile << ",";
                }
                outfile << "\n";
            }
        }
        outfile.close();
        std::cout << "处理完成: " << infile_path << " -> " << outfile_path << std::endl;
    }
    return 0;
}
